<?php
/**
 * Joomla! 1.5 component JoomGPSTracks
 *
 * @version $Id: download.php 2009-06-24 15:40:19 svn $
 * @author Michael Pfister
 * @package joomGPStracks
 * @subpackage frontend
 * @license GNU/GPL
 * @filesource
 *
 */

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class JoomgpstracksModelDownload extends JModel  {
    /**
     *
     * @global object $mainframe
     * @param integer $id
     * @param string $format
     * @return string
     */
    function download($id, $format, $track)  {
        global $mainframe;

        jimport('joomla.filesystem.file');
        $file = "./components/com_joomgpstracks/uploads/".$track->file;
        $ext = JFile::getExt($file['name']);
        $gps = new gpsClass();
        $coords = $gps->getCoords($file);
        
        switch($format)  {
            case "kml":
                $file = "";
                $file .= "<?xml version='1.0' encoding='UTF-8'?>\n";
                $file .= "<kml xmlns='http://www.opengis.net/kml/2.2'>\n";
                $file .= "<Document>\n";
                $file .= "  <name>".$track->title."</name>\n";
                $file .= "  <description>Generated by ".$mainframe->getCfg('sitename')." ".JURI::base()."</description>\n";
                $file .= "  <Folder>\n";
                $file .= "      <Placemark>\n";
                $file .= "          <name>".$track->title."</name>\n";
                $file .= "          <description>".htmlentities($track->description)."</description>\n";
                $file .= "          <Point>\n";
                $file .= "              <coordinates>".$track->start_e.",".$track->start_n.",0.0000000</coordinates>\n";
                $file .= "          </Point>\n";
                $file .= "      </Placemark>\n";
                $file .= "      <Placemark>\n";
                $file .= "          <name>".$track->title."</name>\n";
                $file .= "          <description>Generated by ".$mainframe->getCfg('sitename')." ".JURI::base()."</description>\n";
                $file .= "          <MultiGeometry>\n";
                $file .= "              <LineString>\n";
                $file .= "                  <coordinates>";
                for($i=0, $n=count($coords); $i<$n; $i++)  {
                    $coord = &$coords[$i];
                    if($i != $n-1) {
                        $file .= $coord[0].",".$coord[1].",".$coord[2]."\n";
                    } else {
                        $file .= $coord[0].",".$coord[1].",".$coord[2]."</coordinates>\n";
                    }
                }
                $file .= "              </LineString>\n";
                $file .= "          </MultiGeometry>\n";
                $file .= "      </Placemark>\n";
                $file .= "  </Folder>\n";
                $file .= "</Document>\n";
                $file .= "</kml>\n";

                return $file;
            break;

            case "gpx":
                $file = "";
                $file .= "<?xml version='1.0' encoding='UTF-8' ?>\n";
                $file .= "<gpx xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd'>\n";
                $file .= "  <metadata>\n";
                $file .= "      <name>".$track->title."</name>\n";
                $file .= "      <copyright author='".$mainframe->getCfg('sitename')."' />\n";
                $file .= "      <link href='".JURI::base()."index.php?option=com_joomgpstracks&amp;view=track&amp;layout=track&amp;id=".$id."'>\n";
                $file .= "          <text>".$track->title." on ".$mainframe->getCfg('sitename')."</text>\n";
                $file .= "      </link>\n";
                $file .= "  </metadata>\n";
                $file .= "  <trk>\n";
                $file .= "      <name>".$track->title."</name>\n";
                $file .= "      <link href='".JURI::base()."index.php?option=com_joomgpstracks&amp;view=track&amp;layout=track&amp;id=".$id."' />\n";
                $file .= "          <trkseg>\n";
                for($i=0, $n=count($coords); $i<$n; $i++)  {
                    $coord = &$coords[$i];
                        $file .= "              <trkpt lat='".$coord[1]."' lon='".$coord[0]."'>\n";
                        $file .= "                  <ele>".$coord[2]."</ele>\n";
                        if($coord[3] != NULL):
                            $file .= "                  <time>".$coord[3]."</time>\n";
                        endif;
                        $file .= "              </trkpt>\n";
                }
                $file .= "          </trkseg>\n";
                $file .= "      </trk>\n";
                $file .= "</gpx>\n";
                
                return $file;
            break;

            case "tcx":
                $file ="";
                $file .= "<?xml version='1.0' encoding='UTF-8' standalone='no' ?>\n";
                $file .= "<TrainingCenterDatabase xmlns='http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2 http://www.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd'>\n";
                $file .= "  <Folders/>\n";
                $file .= "  <Courses>\n";
                $file .= "    <Course>\n";
                $file .= "      <Name>".$track->title."</Name>\n";
                $file .= "        <Lap>\n";
                $file .= "          <TotalTimeSeconds></TotalTimeSeconds>\n";
                $file .= "          <DistanceMeters>".($track->distance * 1000)."</DistanceMeters>\n";
                $file .= "              <BeginPosition>\n";
                $file .= "                  <LatitudeDegrees>".$track->start_e."</LatitudeDegrees>\n";
                $file .= "                  <LongitudeDegrees>".$track->start_n."</LongitudeDegrees>\n";
                $file .= "              </BeginPosition>\n";
                $file .= "              <EndPosition>\n";
                $file .= "                  <LatitudeDegrees></LatitudeDegrees>\n";
                $file .= "                  <LongitudeDegrees></LongitudeDegrees>\n";
                $file .= "              </EndPosition>\n";
                $file .= "              <AverageHeartRateBpm xsi:type='HeartRateInBeatsPerMinute_t'>\n";
                $file .= "                  <Value></Value>\n";
                $file .= "              </AverageHeartRateBpm>\n";
                $file .= "              <MaximumHeartRateBpm xsi:type='HeartRateInBeatsPerMinute_t'>\n";
                $file .= "                  <Value></Value>\n";
                $file .= "              </MaximumHeartRateBpm>\n";
                $file .= "              <Intensity>Active</Intensity>\n";
                $file .= "         </Lap>\n";
                $file .= "         <Track>\n";
        for($i=0, $n=count($coords); $i<$n; $i++)  {
            $coord = &$coords[$i];
                $file .= "              <Trackpoint>\n";
                $file .= "                  <Time>".$coord[3]."</Time>\n";
                $file .= "                  <Position>\n";
                $file .= "                      <LatitudeDegrees>".$coord[1]."</LatitudeDegrees>\n";
                $file .= "                      <LongitudeDegrees>".$coord[0]."</LongitudeDegrees>\n";
                $file .= "                  </Position>\n";
                $file .= "                  <AltitudeMeters>".$coord[2]."</AltitudeMeters>\n";
                $file .= "                  <DistanceMeters>0</DistanceMeters>\n";
                $file .= "                  <HeartRateBpm xsi:type='HeartRateInBeatsPerMinute_t'>\n";
                $file .= "                      <Value>".$coord[4]."</Value>\n";
                $file .= "                  </HeartRateBpm>\n";
                $file .= "                  <SensorState>Absent</SensorState>\n";
                $file .= "              </Trackpoint>\n";
        }
                $file .= "      </Track>\n";
                $file .= "    </Course>\n";
                $file .= "  </Courses>\n";
                $file .= "  <Author xsi:type='Application_t'>\n";
                $file .= "      <Name>".$mainframe->getCfg('sitename')."</Name>\n";
                $file .= "      <Build>\n";
                $file .= "        <Version>\n";
                $file .= "          <VersionMajor>2</VersionMajor>\n";
                $file .= "          <VersionMinor>2</VersionMinor>\n";
                $file .= "          <BuildMajor>0</BuildMajor>\n";
                $file .= "          <BuildMinor>0</BuildMinor>\n";
                $file .= "        </Version>\n";
                $file .= "        <Type>Alpha</Type>\n";
                $file .= "        <Time>".$track->date."</Time>\n";
                $file .= "        <Builder>".$track->user."</Builder>\n";
                $file .= "      </Build>\n";
                $file .= "      <LangID>EN</LangID>\n";
                $file .= "      <PartNumber>".$track->id."</PartNumber>\n";
                $file .= "   </Author>\n";
                $file .= "</TrainingCenterDatabase>\n";

                return $file;
                break;
        }

    }

    /**
     *
     * @global object $mainframe
     * @param integer $id
     * @return object
     */
    function getTrack($id)  {
    global $mainframe;

    $db =& JFactory::getDBO();

    $query = "SELECT a.*, b.title AS cat, b.image AS image, c.username AS user"
            . "\n FROM #__gps_tracks AS a"
            . "\n LEFT JOIN #__gps_cats AS b ON a.catid=b.id"
            . "\n LEFT JOIN #__users AS c ON a.uid=c.id"
            . "\n WHERE a.id='" .$id. "'";

    $db->setQuery($query);
    $track = $db->loadObject();

    return $track;
    }

}
?>
